#!/opt/php/bin/php -d open_basedir=
<?php
require_once 'TVB/Message/BaseConsumer.php';
require_once 'TVB/ImageInfrastructure/Server/requires.php';

class ExportImageConsumer extends BaseConsumer {
	const CONFIG_FILE = '/data/image_infrastructure/config/export_image/consumer.xml';

	protected function process(Stomp_Frame $message) {
		// record start time
		$start_time = microtime(TRUE);
		echo date('Y-m-d H:i:s') . ": Start exporting image source {$message->map->export_id} ...\n";

		// gather info
		$login_id = $message->map->login_id;
		$export_id = $message->map->export_id;
		$destination_id = $message->map->destination_id;
		$callback_url = $message->map->callback_url;

		if ($export_id <= 0) {
			return TRUE;
		}
		
		// export
		ImageInfrastructure::Export($export_id);

		// enter distribute queue
		if ($destination_id) {
			ImageInfrastructure::QueueDistribute($login_id, $export_id, $destination_id, $callback_url);
		}
	
		// record end time
		$end_time = microtime(TRUE);
		$time_elapsed = $end_time - $start_time;
		echo date('Y-m-d H:i:s') . ": Export id {$export_id} completed. Time used: {$time_elapsed}s\n";

		return TRUE;
	}
}

$cons = new ExportImageConsumer(ExportImageConsumer::CONFIG_FILE, 'iiExportImage');
$cons->run();
?>
